﻿using System;
using System.Collections.ObjectModel;

namespace Automator.Runtime
{   
    /// <summary>
    /// Class that implements an item of the application log
    /// </summary>
    public class LogItem
    {
        public string Message { get; private set; }
        public LogMessageType MessageType { get; private set; }
        public DateTime MessageTime { get; private set; }
        
        public LogItem(string message, LogMessageType messageType)
        {
            Message = message;
            MessageType = messageType;
            MessageTime = DateTime.Now;
        }
    }
    
    /// <summary>
    /// Class that implements managing of the workflow log
    /// </summary>
    public class Logger
    {
        private ObservableCollection<LogItem> log = new ObservableCollection<LogItem>();

        public ReadOnlyObservableCollection<LogItem> Instance
        {
            get { return new ReadOnlyObservableCollection<LogItem>(log); }
        }
        
        /// <summary>
        /// Adds a LogItem object to log
        /// </summary>
        /// <param name="item">LogItem to add</param>
        public void AddLog(LogItem item)
        {
            log.Add(item);
            if (LogItemAdded != null)
                LogItemAdded();
        }
        
        /// <summary>
        /// Implicitly adds a new LogItem with provided LogMessageType and message text to log
        /// </summary>
        /// <param name="messageType">Type of the message</param>
        /// <param name="message">Message text</param>
        public void AddLog(LogMessageType messageType, string message)
        {
            AddLog(new LogItem(message, messageType));
        }
        
        public void Clear()
        {
            log.Clear();
        }
        
        public event LogItemAddedHandler LogItemAdded;
    }
}
